/**
 * 认证相关的 TypeScript 类型定义
 */

// 基础用户信息
export interface UserInfo {
  id: string
  username: string
  email: string
  phone?: string
  avatar?: string
  nickname?: string
  createdAt: string
  updatedAt: string
}

// 登录请求参数
export interface LoginRequest {
  account: string  // 账号（用户名/邮箱/手机号）
  password: string
  rememberMe?: boolean
}

// 登录响应数据
export interface LoginResponse {
  accessToken: string
  refreshToken: string
  tokenType: string
  expiresIn: number  // 过期时间（秒）
  userInfo: UserInfo
}

// 注册请求参数
export interface RegisterRequest {
  username: string
  email: string
  phone?: string
  password: string
  confirmPassword: string
  verifyCode: string
  verifyType: 'email' | 'phone'
}

// 重置密码请求参数
export interface ResetPasswordRequest {
  account: string  // 邮箱或手机号
  verifyCode: string
  newPassword: string
  confirmPassword: string
  verifyType: 'email' | 'phone'
}

// 发送验证码请求参数
export interface SendCodeRequest {
  account: string  // 邮箱或手机号
  type: 'email' | 'phone'
}

// 刷新令牌请求参数
export interface RefreshTokenRequest {
  refreshToken: string
}

// API 通用响应格式
export interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
  success?: boolean  // 可选字段，兼容不同的后端响应格式
  timestamp?: number // 可选字段
}

// 认证状态
export interface AuthState {
  isAuthenticated: boolean
  accessToken: string | null
  refreshToken: string | null
  tokenType: string
  expiresAt: number | null
  userInfo: UserInfo | null
  loading: boolean
}

// 表单验证规则类型
export interface FormRule {
  required?: boolean
  message?: string
  pattern?: RegExp
  min?: number
  max?: number
  validator?: (rule: any, value: any, callback: (error?: Error) => void) => void
  trigger?: 'blur' | 'change'
}

// 表单验证规则集合
export interface FormRules {
  [key: string]: FormRule[]
}

// 验证码相关状态
export interface VerifyCodeState {
  sending: boolean
  countdown: number
  canSend: boolean
}

// 路由元信息
export interface RouteMeta extends Record<PropertyKey, unknown> {
  requiresAuth?: boolean
  requiresGuest?: boolean
  title?: string
  layout?: string
}

// HTTP 错误响应
export interface HttpError {
  code: number
  message: string
  details?: any
}

// 认证错误类型
export type AuthErrorType = 
  | 'INVALID_CREDENTIALS'
  | 'USER_NOT_FOUND'
  | 'USER_ALREADY_EXISTS'
  | 'TOKEN_EXPIRED'
  | 'TOKEN_INVALID'
  | 'VERIFICATION_CODE_INVALID'
  | 'VERIFICATION_CODE_EXPIRED'
  | 'NETWORK_ERROR'
  | 'UNKNOWN_ERROR'

// 认证错误
export interface AuthError {
  type: AuthErrorType
  message: string
  code?: number
  details?: any
}

// 表单数据类型
export interface LoginForm {
  account: string
  password: string
  rememberMe: boolean
}

export interface RegisterForm {
  username: string
  email: string
  phone: string
  password: string
  confirmPassword: string
  verifyCode: string
  verifyType: 'email' | 'phone'
}

export interface ResetPasswordForm {
  account: string
  verifyCode: string
  newPassword: string
  confirmPassword: string
  verifyType: 'email' | 'phone'
}

// 本地存储键名
export enum StorageKeys {
  ACCESS_TOKEN = 'access_token',
  REFRESH_TOKEN = 'refresh_token',
  TOKEN_TYPE = 'token_type',
  EXPIRES_AT = 'expires_at',
  USER_INFO = 'user_info',
  REMEMBER_ME = 'remember_me'
}

// API 端点
export enum ApiEndpoints {
  LOGIN = '/auth/login',
  REGISTER = '/auth/register',
  LOGOUT = '/auth/logout',
  REFRESH = '/auth/refresh',
  SEND_CODE = '/auth/send-code',
  RESET_PASSWORD = '/auth/reset-password',
  VALIDATE = '/auth/validate',
  USER_INFO = '/auth/user-info'
}
